<template>
  <!-- 计划维修表单弹窗 -->
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    :title="props.title"
    width="70%"
    draggable
    @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="160">
      <el-row>
        <el-col :span="12">
          <el-form-item label="计划编码" prop="planCode">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.planCode"
              placeholder="请输入计划编码"
              clearable
            />

            <span v-else>{{ formData.planCode }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="计划名称" prop="planName">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.planName"
              placeholder="请输入计划名称"
              clearable
            />

            <span v-else>{{ formData.planName }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="维修类型" prop="repairType">
            <el-select
              style="width: 100%"
              v-if="!props.isReadonly"
              v-model="formData.repairType"
              placeholder="请选择维修类型"
              clearable
            >
              <el-option
                v-for="item in dictionaryStore.filterDictList('EamRepairType')"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>

            <span v-else>{{
              dictionaryStore.filterDictName(
                "EamRepairType",
                formData.repairType
              )
            }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="计划开始日期" prop="planStartTime">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.planStartTime"
              placeholder="请输入计划开始日期"
              clearable
            />

            <span v-else>{{ formData.planStartTime }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="计划结束日期" prop="planEndTime">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.planEndTime"
              placeholder="请输入计划结束日期"
              clearable
            />

            <span v-else>{{ formData.planEndTime }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="提醒时间" prop="planRemindTime">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.planRemindTime"
              placeholder="请输入提醒时间"
              clearable
            />

            <span v-else>{{ formData.planRemindTime }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="提醒时间类型" prop="planRemindTimeType">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.planRemindTimeType"
              placeholder="请输入提醒时间类型"
              clearable
            />

            <span v-else>{{ formData.planRemindTimeType }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备管理部门" prop="deviceDepartment">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.deviceDepartment"
              placeholder="请输入设备管理部门"
              clearable
            />

            <span v-else>{{ formData.deviceDepartment }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="维修执行部门" prop="repairDepartment">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.repairDepartment"
              placeholder="请输入维修执行部门"
              clearable
            />

            <span v-else>{{ formData.repairDepartment }}</span>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="是否启用" prop="enabled">
            <template v-if="!props.isReadonly">
              <el-radio-group v-model="formData.enabled">
                <el-radio :label="1">是</el-radio>
                <el-radio :label="0">否</el-radio>
              </el-radio-group>
            </template>
            <span v-else>{{
              dictionaryStore.filterDictName("YesNo", formData.enabled)
            }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="备注" prop="remarks">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.remarks"
              placeholder="请输入备注"
              clearable
            />

            <span v-else>{{ formData.remarks }}</span>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "EAMEAMREPAIRPLANADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    setTimeout(() => {
      formRef.value.clearValidate();
    }, 50);
  }
);

const rules = reactive({
  version: [],
  planCode: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  planName: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  repairType: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  planStartTime: [],
  planEndTime: [],
  planRemindTime: [],
  planRemindTimeType: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  deviceDepartment: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  repairDepartment: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  remarks: [
    { min: 1, max: 255, message: "长度在 1 到 255 个字符", trigger: "blur" },
  ],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  version: "",
  planCode: "",
  planName: "",
  repairType: "",
  planStartTime: "",
  planEndTime: "",
  planRemindTime: "",
  planRemindTimeType: "",
  deviceDepartment: "",
  repairDepartment: "",
  remarks: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  enabled: 1,
  deleted: "",
  assetKind: 1,
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        version,
        planCode,
        planName,
        repairType,
        planStartTime,
        planEndTime,
        planRemindTime,
        planRemindTimeType,
        deviceDepartment,
        repairDepartment,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        enabled,
        deleted,
        assetKind,
      } = newVal;
      Object.assign(formData, {
        id,
        version,
        planCode,
        planName,
        repairType,
        planStartTime,
        planEndTime,
        planRemindTime,
        planRemindTimeType,
        deviceDepartment,
        repairDepartment,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        enabled,
        deleted,
        assetKind,
      });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.version = "";
  formData.planCode = "";
  formData.planName = "";
  formData.repairType = "";
  formData.planStartTime = "";
  formData.planEndTime = "";
  formData.planRemindTime = "";
  formData.planRemindTimeType = "";
  formData.deviceDepartment = "";
  formData.repairDepartment = "";
  formData.remarks = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
  formData.enabled = "";
  formData.deleted = "";
  formData.assetKind = "";
}

const loading = ref(false);

/** 计划维修保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/eam/equrepairplan/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
